package com.ilop.sthome.ui.adapter.device;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-07-09.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备类型选择适配器
 */
public class ChooseTypeAdapter extends RecyclerView.Adapter<ChooseTypeAdapter.ItemHolder> {

    private Context mContext;
    private List<String> mTypeList;
    private int mPosition = -1;

    public ChooseTypeAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setTypeList(List<String> mTypeList) {
        this.mTypeList = mTypeList;
        notifyDataSetChanged();
    }

    public void setPosition(int position){
        this.mPosition = position;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_choose_room, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        String name = mTypeList.get(i);
        itemHolder.mName.setText(name);
        if (mPosition == i){
            itemHolder.mName.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            itemHolder.mName.setBackgroundResource(R.drawable.dialog_round_bg3);
        }else {
            itemHolder.mName.setTextColor(ContextCompat.getColor(mContext, R.color.text_title));
            itemHolder.mName.setBackgroundResource(R.drawable.dialog_round_bg);
        }
        View.OnClickListener choose_room = view -> LiveDataBus.get().with(CommonId.CHOOSE_DEVICE_TYPE).setValue(i);
        itemHolder.mName.setOnClickListener(choose_room);
    }

    @Override
    public int getItemCount() {
        return mTypeList == null ? 0 : mTypeList.size();
    }

     static class ItemHolder extends RecyclerView.ViewHolder {
         @BindView(R.id.tv_room_name)
         TextView mName;
         ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
